package com.fifth.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fifth.commodity.vo.BrandVo;
import com.fifth.commodity.vo.DepositStatusVo;
import com.fifth.commodity.vo.DepositVo;
import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Deposit;
import com.fifth.commodity.mapper.DepositMapper;
import com.fifth.commodity.service.DepositService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
@Service
public class DepositServiceImpl implements DepositService {

    @Autowired
    private DepositMapper depositMapper;
    @Override
    public CommonResult SelectAll(Deposit deposit) {

        List<Deposit> deposits = depositMapper.selectAll(deposit);
        return new CommonResult(2000,"查询成功",deposits);
    }


    @Override
    public CommonResult DepositDelete(String id) {
        int i = depositMapper.deleteById(id);
        return new CommonResult(2000,"删除成功",i);
    }

    @Override
    public CommonResult DepositInsert(Deposit deposit) {
        int insert = depositMapper.insert(deposit);
        return new CommonResult(2000,"添加成功",insert);
    }

    @Override
    public CommonResult DepositUpdate(Deposit deposit) {
        int i = depositMapper.updateById(deposit);
        return new CommonResult(2000,"修改成功",i);
    }

    /**
     * 根据条件分页查询所有储藏方式
     * @param pageCurrent
     * @param pageSize
     * @param depositVo
     * @return
     */
    @Override
    public CommonResult findDepositCondition(Integer pageCurrent, Integer pageSize, DepositVo depositVo) {
        IPage<Deposit> page = new Page<>(pageCurrent,pageSize);
        QueryWrapper<Deposit> queryWrapper = new QueryWrapper<>();
        if (depositVo.getDepositid()!=null){
            queryWrapper.eq("id",depositVo.getDepositid());
        }
        if (depositVo.getStatus()!=null){
            queryWrapper.eq("status",depositVo.getStatus());
        }
        if (depositVo.getDepositid()==null && depositVo.getStatus()==null){
            queryWrapper = null;
        }
        IPage<Deposit> page1 = depositMapper.selectPage(page, queryWrapper);
        return new CommonResult(2000,"查询成功",page1);
    }

    /**
     * 查询所有储藏方式
     * @return
     */
    @Override
    public CommonResult findAllDeposit() {
        List<Deposit> deposits = depositMapper.selectList(null);
        return new CommonResult(2000,"查询成功",deposits);
    }

    /**
     * 根据id删除储藏类型
     * @param id
     * @return
     */
    public CommonResult deleteDeposit(Integer id){
        int i = depositMapper.deleteById(id);
        if (i==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }

    /**
     * 修改审核状态
     * @param depositStatusVo
     * @return
     */
    @Override
    public CommonResult updateStatus(DepositStatusVo depositStatusVo) {
        Deposit deposit = new Deposit();
        deposit.setStatus(depositStatusVo.getState());
        deposit.setCheckContent(depositStatusVo.getCheckContent());
        QueryWrapper<Deposit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",depositStatusVo.getId());
        int update = depositMapper.update(deposit, queryWrapper);
        if (update==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }
}
